package de.exti.replayparser;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Replay {
	private File replay;
	
	private int version;
	private int buildNo;
	private int length;

	public Replay(File replayFile) {
		this.replay = replayFile;
	}

	public void load() {
		try {
			FileInputStream in = new FileInputStream(replay);
			loadHeader(in);
			loadPlayers(in);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void loadHeader(FileInputStream in) {
		try {
			DataInputStream reader = new DataInputStream(in);
			// main header (php)
			// 'a28intro/Vheader_size/Vc_size/Vheader_v/Vu_size/Vblocks'
			byte[] b = new byte[28];
			reader.read(b);
			for (byte bb : b) {
				System.out.print((char) bb); 	// Warcraft III recorded game 28chars
			}
			int headerSize = Integer.reverseBytes(reader.readInt()); 	// header size
			reader.readInt(); 					// size of compressed file
			int versionFlag = Integer.reverseBytes(reader.readInt()); 	// versionFlag (1 for > 1.07)
			
			reader.readInt(); 					// overall size of decompressed data (excluding header)
			int nBlocks = Integer.reverseBytes(reader.readInt()); 	// number of compressed data blocks in file

			
			// sub header
			// a4ident/Vmajor_v/vbuild_v/vflags/Vlength/Vchecksum
			// V = 32 Bit, Little Endian
			// v = 32 Bit, Big Endian
			String versionIdentifier = ParserUtility.stringFromUInt(reader
					.readInt());				// version identifier "W3XP" for tft/dota
			version = Integer.reverseBytes(reader.readInt());			// patch 1.xx
			buildNo = reader.readShort();		// build no, returns bullshit so far
			reader.skipBytes(1);
			int gameType = reader.read();		// game type. 0x0080 for lan instead of 0x8000 
			length = Integer.reverseBytes(reader.readInt());
			reader.readInt(); 					// checksum
			
			System.out.println("\n--------------HEADER INFO--------------");
			System.out.println("Header Size: "
					+ Integer.toHexString(headerSize) + "\n" + "Version Flag: "
					+ Integer.toHexString(versionFlag) + "\n"
					+ "Number of Compressed Blocks: " + nBlocks + "\n"
					+ "Version Identifier: " + versionIdentifier + "\n"
					+ "Version Number: 1." + version + "\n"
					+ "Game Type: " + gameType + "\n" 
					+ "Replay Time: " + Converter.convertTime(length));
			
		} catch (Exception e) {

		}
	}

	private void loadPlayers(FileInputStream in) {
		// # |   Size   | Name
        //---+----------+--------------------------
        // 1 |   4 byte | Unknown (0x00000110 - another record id?)
        // 2 | variable | PlayerRecord (see 4.1)
        // 3 | variable | GameName (null terminated string) (see 4.2)
        // 4 |   1 byte | Nullbyte
        // 5 | variable | Encoded String (null terminated) (see 4.3)
        //   |          |  - GameSettings (see 4.4)
        //   |          |  - Map&CreatorName (see 4.5)
        // 6 |   4 byte | PlayerCount (see 4.6)
        // 7 |   4 byte | GameType (see 4.7)
        // 8 |   4 byte | LanguageID (see 4.8)
        // 9 | variable | PlayerList (see 4.9)
        //10 | variable | GameStartRecord (see 4.11)

	}
	
}
